Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [
      {
        id: 1,
        imgUrl: "cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/banner/banner1.png",
        link: '/pages/revolutionDate/revolutionDate',
        events: []
      },
      {
        id: 2,
        imgUrl: "cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/banner/banner2.png",
        link: '/pages/revolutionDate/revolutionDate',
        events: []
      },
      {
        id: 3,
        imgUrl: "cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/banner/banner3.png",
        link: '/pages/revolutionDate/revolutionDate',
        events: []
      },
      {
        id: 4,
        imgUrl: "cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/banner/banner4.png",
        link: '/pages/revolutionDate/revolutionDate',
        events: []
      }
    ],
    iconList:[
      {
        id:1,
        imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/icon/icon1.png',
        text:'学习书库',
        path:'/pages/studyLibrary/studyLibrary'
      },
      {
        id:2,
        imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/icon/icon2.png',
        text:'红色展馆',
        path:'/pages/redPavilion/redPavilion'
      },
      {
        id:3,
        imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/icon/icon3.png', 
        text:'党史今日',
        path:'/pages/historyToday/historyToday'
      },
      {
        id:4,
        imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/icon/icon4.png',
        text:'每日一练',
        path:'/pages/practiceDay/practiceDay'
      },
      {
        id:5,
        imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/icon/icon5.png',
        text:'学习计划',
        path:'/pages/studyPlan/studyPlan'
      }
    ],
    heroList:[{
      id:1,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero1.jpg',
      name: "毛泽东"
    },
    {
      id:2,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero2.jpg',
      name: "董存瑞"
    },
    {
      id:3,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero3.jpg',
      name: "黄继光"
    },
    {
      id:4,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero4.jpg',
      name: "刘胡兰"
    },
    {
      id:5,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero5.jpg',
      name: "邱少云"
    },
    {
      id:6,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero6.jpg',
      name: "杨靖宇"
    },
    {
      id:7,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero7.jpg',
      name: "赵尚志"
    },
    {
      id:8,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero8.jpg',
      name: "张思德"
    },
    {
      id:9,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero9.jpg',
      name: "江竹筠"
    },
    {
      id:10,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero10.jpg',
      name: "王进喜"
    },
    {
      id:11,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero11.jpg',
      name: "雷锋"
    }
  ],
    hero:{
      id:1,
      imgUrl:'cloud://cloud1-4g4aia8u2173a4df.636c-cloud1-4g4aia8u2173a4df-1322250303/img/index/hero/hero1.jpg',
      name: "毛泽东"
    },
    isExpend: false,
    poetryList: [],
    poetry:null
  },
   // 轮播图点击事件
   onBannerClick(e) {
    const id = e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: `/pages/revolutionDate/revolutionDate?id=${id}`
    })
  },
  navigateToPage(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.path,
    })
  },
  handleExpend(e) {
    this.setData({
      isExpend:!this.data.isExpend
    });
  },
  // 新增获取诗歌数据方法
  getPoetryData() {
    const db = wx.cloud.database()
    db.collection('poetryList')
      .get()
      .then(res => {
        if (res.data && res.data.length > 0) {
          // 新增随机选择逻辑
          const randomIndex = Math.floor(Math.random() * res.data.length)
          this.setData({
            poetryList: res.data,  // 保留完整列表
            poetry: res.data[randomIndex] // 随机选取一个
          })
        } else {
          wx.showToast({
            title: '暂无诗歌数据',
            icon: 'none'
          })
        }
      })
      .catch(err => {
        console.error('数据库数据获取失败', err)
        wx.showToast({
          title: '数据加载失败',
          icon: 'none'
        })
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPoetryData()
    const hero = this.data.heroList[
      Math.floor(Math.random() * this.data.heroList.length)
    ];
    this.setData({
      hero
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})